<template>
	<view class="gsxx-page">
		<!-- 自定义导航栏 -->
		<view class="custom-navbar">
			<view class="navbar-content">
				<image class="back-icon" src="https://apizx01.sc798.com/Resource/home/left.png" mode="aspectFit" @click="goBack"></image>
				<text class="navbar-title">{{ navbarTitle }}</text>
				<view class="navbar-placeholder"></view>
			</view>
		</view>
		
		<!-- 页面内容 -->
		<view class="page-content">
			<!-- 加载状态 -->
			<view v-if="loading" class="loading-container">
				<text class="loading-text">加载中...</text>
			</view>
			
			<!-- 工商信息内容 -->
			<view v-else>
				<view class="info-box">
					<view class="info-item">
						<text class="info-label">法人名称</text>
						<view class="info-value-box">
							<view class="box">{{ companyInfo.operName ? companyInfo.operName.charAt(0) : '法' }}</view>
							<text class="info-value">{{ companyInfo.operName || '暂无' }}</text>
						</view>
					</view>
				</view>
				<view class="info">
					<view class="info-item">
						<text class="info-label">成立日期</text>
						<text class="info-value">{{ companyInfo.startDate || '暂无' }}</text>
					</view>
					<view class="info-item">
						<text class="info-label">登记状态</text>
						<text class="info-value">{{ companyInfo.status || '暂无' }}</text>
					</view>
					<view class="info-item">
						<text class="info-label">注册资本</text>
						<text class="info-value">{{ companyInfo.registCapi || '暂无' }}</text>
					</view>
					<view class="info-item">
						<text class="info-label">实缴资本</text>
						<text class="info-value">{{ companyInfo.registCapi || '暂无' }}</text>
					</view>
					<view class="info-item">
						<text class="info-label">统一社会信用代码</text>
						<text class="info-value">{{ companyInfo.creditCode || '暂无' }}</text>
					</view>
					<view class="info-item">
						<text class="info-label">组织机构代码</text>
						<text class="info-value">{{ companyInfo.orgNo || '暂无' }}</text>
					</view>
				</view>
				<!-- 工商信息详情 -->
				<view class="info-detail">
					<view class="info-item">
						<text class="info-label">工商注册号</text>
						<text class="info-value">{{ companyInfo.regNo || '暂无' }}</text>
					</view>
					<view class="info-item">
						<text class="info-label">企业类型</text>
						<text class="info-value">{{ companyInfo.econKind }}</text>
					</view>
					<view class="info-item">
						<text class="info-label">经营期限</text>
						<text class="info-value">{{ companyInfo.termStart || '暂无' }} 至 {{ companyInfo.termEnd || '无固定期限' }}</text>
					</view>
					<view class="info-item">
						<text class="info-label">国标行业</text>
						<text class="info-value">{{ companyInfo.industryNameNew || '暂无' }}</text>
					</view>
					<view class="info-item">
						<text class="info-label">人员规模</text>
						<text class="info-value">{{ companyInfo.colleguesNum }}</text>
					</view>
					<view class="info-item">
						<text class="info-label">注册地址</text>
						<text class="info-value">{{ companyInfo.address || '暂无' }}</text>
					</view>
					<view class="info-item">
						<text class="info-label">登记机关</text>
						<text class="info-value">{{ companyInfo.belongOrg || '暂无' }}</text>
					</view>
					<view class="info-item">
						<text class="info-label">核准日期</text>
						<text class="info-value">{{ companyInfo.checkDate }}</text>
					</view>
					<view class="info-item">
						<text class="info-label">英文名称</text>
						<text class="info-value">{{ companyInfo.englishName || '-' }}</text>
					</view>
					<view class="info-item">
						<text class="info-label">所属地区</text>
						<text class="info-value">{{ companyInfo.city }}</text>
					</view>
					<view class="info-item business-scope">
						<text class="info-label">经营范围</text>
						<view class="info-value">
							<text class="scope-content">{{ companyInfo.scope || '暂无' }}</text>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
import { ref, onMounted } from 'vue'

// 响应式数据
const navbarTitle = ref('工商信息')
const companyInfo = ref({})
const loading = ref(false)

// 返回上一页
const goBack = () => {
	uni.navigateBack()
}

// 调用API获取工商信息
const fetchCompanyInfo = async (requestData) => {
	try {
		loading.value = true
		console.log('发送工商信息请求参数:', requestData)
		
		// 发送POST请求到 /enterprise/basic/basic
		const response = await uni.request({
			url: 'https://apizx01.sc798.com/api/proxy/enterprise/basic/basic',
			method: 'POST',
			data: requestData,
			header: {
				'content-type': 'application/x-www-form-urlencoded'
			}
		})
		
		console.log('工商信息响应:', response)
		
		// 处理响应数据
		if (response.data.code === 200) {
			companyInfo.value = response.data.rows[0] || {}
			console.log('工商信息数据:', companyInfo.value)
		} else {
			console.log('响应状态码不是200:', response.data?.code)
			uni.showToast({
				title: '获取工商信息失败',
				icon: 'none'
			})
		}
	} catch (error) {
		console.error('获取工商信息失败:', error)
		uni.showToast({
			title: '获取工商信息失败',
			icon: 'none'
		})
	} finally {
		loading.value = false
	}
}

// 页面加载时获取参数并调用API
onMounted(() => {
	// 获取页面参数
	const pages = getCurrentPages()
	const currentPage = pages[pages.length - 1]
	const options = currentPage.options
	
	if (options.title) {
		navbarTitle.value = decodeURIComponent(options.title)
	}
	
	// 获取requestData参数
	if (options.requestData) {
		try {
			const requestData = JSON.parse(decodeURIComponent(options.requestData))
			console.log('接收到的requestData:', requestData)
			fetchCompanyInfo(requestData)
		} catch (error) {
			console.error('解析requestData失败:', error)
			uni.showToast({
				title: '参数解析失败',
				icon: 'none'
			})
		}
	}
})
</script>

<style lang="scss" scoped>
.gsxx-page {
	width: 100%;
	min-height: 100vh;
	background-color: #fff;
}

.custom-navbar {
	position: fixed;
	top: 0;
	left: 0;
	right: 0;
	z-index: 1000;
	background-color: #ffffff;
	
	.navbar-content {
		display: flex;
		align-items: center;
		height: 210rpx;
		padding: 0 24rpx;
		padding-top: 44rpx;
		box-sizing: border-box;
		
		.back-icon {
			width: 48rpx;
			height: 48rpx;
		}
		
		.navbar-title {
			flex: 1;
			text-align: center;
			font-family: 'Alibaba PuHuiTi', sans-serif;
			font-weight: 500;
			font-size: 32rpx;
			color: #000000;
		}
		
		.navbar-placeholder {
			width: 32rpx;
			height: 32rpx;
		}
	}
}

.page-content {
	padding-top: 210rpx;
	padding-bottom: 40rpx;
}

.loading-container {
	display: flex;
	justify-content: center;
	align-items: center;
	height: 400rpx;
	
	.loading-text {
		font-family: 'Alibaba PuHuiTi', sans-serif;
		font-weight: 400;
		font-size: 28rpx;
		color: #666666;
	}
}

.info-box {
	width: 704rpx;
	padding: 0 0 0 49rpx;
	margin-bottom: 30rpx;
    .info-value-box {
        display: flex;
        align-items: center;
        .box {
            width: 62rpx;
height: 62rpx;
background: #2B6FEA;
border-radius: 20rpx;
font-weight: 400;
font-size: 30rpx;
color: #FFFFFF;
line-height: 62rpx;
text-align: center;
        }
        .info-value{
            margin-left: 20rpx;
        }
    }
}
.info {
	width: 704rpx;
	padding: 0 0 0 49rpx;
	box-sizing: border-box;
	display: flex;
	flex-wrap: wrap;
    .info-item {
       min-width: 265rpx;
    }
}
.info-detail {
	width: 704rpx;
	padding: 0 0 40rpx 49rpx;
	box-sizing: border-box;
	display: flex;
	flex-direction: column;
}

.info-item {
	display: flex;
	flex-direction: column;
	margin-bottom: 20rpx;
	
	&:last-child {
		margin-bottom: 0;
	}
	
	&.business-scope {
		.info-label {
			margin-bottom: 8rpx;
		}
		
		.info-value {
			display: flex;
			flex-direction: column;
			
			.scope-title {
				font-family: 'Alibaba PuHuiTi', sans-serif;
				font-weight: 500;
				font-size: 24rpx;
				color: #000000;
				line-height: 39rpx;
				margin-bottom: 8rpx;
			}
			
			.scope-content {
				font-family: 'Alibaba PuHuiTi', sans-serif;
				font-weight: 400;
				font-size: 24rpx;
				color: #000000;
				line-height: 39rpx;
				margin-bottom: 16rpx;
			}
		}
	}
	
	.info-label {
		font-family: 'Alibaba PuHuiTi', sans-serif;
		font-weight: 400;
		font-size: 24rpx;
		color: #9B9B9B;
		line-height: 39rpx;
		margin-bottom: 8rpx;
	}
	
	.info-value {
		font-family: 'Alibaba PuHuiTi', sans-serif;
		font-weight: 400;
		font-size: 24rpx;
		color: #000000;
		line-height: 39rpx;
	}
}

</style>
